﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace sqlDALgen.Renderers
{
    public class ReplaceRenderer : IRenderer
    {
        private readonly XmlNode _node;

        private readonly String _what;
        private readonly String _to;

        public static IRenderer Get(XmlNode node)
        {
            return new TextRenderer(node);
        }

        public ReplaceRenderer(XmlNode node)
        {
            _node = node;

            _what = node.Attributes["what"].Value.ToString();
            _to = node.Attributes["to"].Value.ToString();
        }

        public string Render(TemplateData data)
        {
            var value = TemplateRenderer.FillData(_node.InnerText, data);

            var whats = _what.Split(',');
            var tos = _to.Split(',');

            for (var i = 0; i < whats.Length; i++)
            {
                if (i < tos.Length)
                    value = value.Replace(whats[i], tos[i]);
            }
            return value;
        }

        public string FileName
        {
            get { throw new NotImplementedException(); }
        }
    }
}
